﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using UniumLocalNetGames.Net;


namespace UniumLocalNetGames.Net
{

    public class IncomingMessageEventArgs : EventArgs
    {
        public UniumLocalMessage message;
        public IncomingMessageEventArgs(UniumLocalMessage mes)
        {
            message = mes;
        }
    }

    public class UniumLocalUdpChat
    {
        private IPHostEntry me;
        public string MyName { get; set; }
        private UniumLocalUdpServer _server;
        public event EventHandler<IncomingMessageEventArgs> IncomingMessage;


        private UniumLocalUdpSender _sender;
        private Timer t;
        public bool autoRefresh = true;
       
        private int autoRefreshTime = 500;
        public int AutoRefreshTime
        {
            get
            {
                return autoRefreshTime;
            }
            set
            {
                if (value < 100)
                {
                    throw new Exception("Слишком маленький интервал!");
                }
                else
                {
                    autoRefreshTime = value;
                    t.Change(0, autoRefreshTime);
                }
            }
        }

     

        public void Refresh()
        {

        }
        public void SendMessege()
        {

        }
        public void OnTimer(object o)
        {
            if (autoRefresh == true)
            {
                Refresh();
            }
       
        }


		public Dictionary<IPEndPoint, string> _onlineClients = new Dictionary<IPEndPoint, string>();

        public UniumLocalUdpChat()
        {
            t = new Timer(OnTimer);
            t.Change(0, autoRefreshTime);

            _server = new UniumLocalUdpServer(this);
            _sender = new UniumLocalUdpSender();

        }

        public string NewMessage(UniumLocalMessage message)
        {
            switch (message.type)
            {
                case MessagesEnum.Hello:

                    if (_onlineClients.Keys.Contains(message.sender))
                        _onlineClients[message.sender] = message.text;  
                    else
                        _onlineClients.Add(message.sender, message.text);
                    RaiseIncomingMessage(new IncomingMessageEventArgs(message));

                    // Ответим ему что мы тут
                    var answer = new UniumLocalMessage();
                    answer.type = MessagesEnum.HelloAnswer;
                    answer.sender = message.sender;
                    answer.text = MyName;
                    _sender.SendMessage(answer);

                    break;
                case MessagesEnum.HelloAnswer:
                    if (_onlineClients.Keys.Contains(message.sender))
                        _onlineClients[message.sender] = message.text;
                    else
                        _onlineClients.Add(message.sender, message.text);
                    break;
                case MessagesEnum.PrivatText:
                    break;
                case MessagesEnum.Text:
                    RaiseIncomingMessage(new IncomingMessageEventArgs(message));
                    break;
                default:
                    break;
            }

            
            return "";
        }

        private void RaiseIncomingMessage(IncomingMessageEventArgs m)
        {
            if (IncomingMessage != null)
                IncomingMessage(this,m);
        }

        public void StartListen()
        {
            _server.StartListenAsync();

            var hello = new UniumLocalMessage();
            hello.type = MessagesEnum.Hello;
            hello.sender = new IPEndPoint(IPAddress.Broadcast, 15000);
            hello.text = MyName;

            _sender.SendMessage(hello);
           
        }


    }
}
